unit StoreTopicF;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, TopicF, ExtCtrls, Grids, DBGrids, DBGridA, StdCtrls, FMTBcd, SqlExpr,
  Provider, DB, DBClient, ComCtrls;

type
  TStoreTopicFrame = class(TTopicFrame)
    Divisions: TSQLDataSet;
    Tree: TTreeView;
    DivisionsOSAKOND: TStringField;
    DivisionsGRUPP: TStringField;
    DivisionsARV: TIntegerField;
    procedure TreeClick(Sender: TObject);
  private
    procedure LoadItems;
  public
    constructor Create(AOwner: TComponent); override;
    procedure OnShow; override;
  end;

const
  DEPT_NODE = 0;
  GROUP_NODE = 1;
var
  StoreTopicFrame: TStoreTopicFrame;

implementation
uses ProgramM, MainFrm, StoreF;

{$R *.dfm}

constructor TStoreTopicFrame.Create(AOwner: TComponent);
begin
  inherited;
end;



procedure TStoreTopicFrame.OnShow;
begin
  inherited;
  if Tree.Items.Count = 0 then
  begin
    LoadItems;
  end;
end;

procedure TStoreTopicFrame.TreeClick(Sender: TObject);
begin
  if Tree.Selected <> nil then
  begin
    if Tree.Selected.Parent = nil then // root
    begin
      MainForm.OpenStoreFrame('*', '*');
    end
    else if Tree.Selected.Parent.Parent = nil then // osakond
    begin
      MainForm.OpenStoreFrame(Tree.Selected.Text, '*');
    end
    else
    begin
      MainForm.OpenStoreFrame(Tree.Selected.Parent.Text, Tree.Selected.Text);
    end;
  end;
end;

procedure TStoreTopicFrame.LoadItems;
var
  PrevDept: String;
  RootNode, DeptNode, Node: TTreeNode;
begin
  Tree.Items.BeginUpdate;
  Tree.Items.Clear;
  Divisions.Open;

  RootNode := Tree.Items.AddChild(nil, StoreF.ALL_DEPTS_ALL_GROUPS);
  RootNode.ImageIndex := 1;
  PrevDept := '';
  DeptNode := nil;


  while not Divisions.Eof do
  begin
    if DivisionsOSAKOND.AsString <> PrevDept then
    begin
      DeptNode := Tree.Items.AddChild(RootNode, DivisionsOSAKOND.AsString);
      DeptNode.ImageIndex := 1;
      DeptNode.ImageIndex := 1;
    end;
    Node := Tree.Items.AddChild(DeptNode, DivisionsGRUPP.AsString);
    Node.ImageIndex := 1;
    Node.ImageIndex := 1;
    PrevDept := DivisionsOSAKOND.AsString;
    Divisions.Next;
  end;
  RootNode.Expand(True);


  Divisions.Close;
  Tree.Items.EndUpdate;
end;

end.
